
var xhr='';
var elUsername =document.getElementById('username'),
    elPassword =document.getElementById('password'),
    elRpassword=document.getElementById('rpassword');
    //elNameMsg  =document.getElementById('usernameFeedback'),

function checkUsername(username){
    if(username===null||username===''||username.length===0){
        document.getElementById("usernameFeedback").innerText='用户名不能为空';
        //elNameMsg.textContent='用户名不能为空';
        return;
    }
    if(username.length<=3){
        document.getElementById("usernameFeedback").innerText='用户名必须大于3'
        //elNameMsg.textContent='用户名必须大于5';
        return;
    }
    //elNameMsg.textContent='';
    sendRequest(username);//发送请求
}
//通过事件监听器绑定
elUsername.addEventListener('blur',function()
{
    var username = elUsername.value.trim();
    checkUsername(username);
});

//回调函数
function process(username){
    if(xhr.readyState===4){
        if(xhr.status===200){
            var responseInfo=xhr.responseText;
            console.log(responseInfo);
            if(responseInfo==="Exists"){

                document.getElementById("usernameFeedback").innerText='用户名已存在';
                //elNameMsg.textContent='用户名已存在';
            }
            else{
                console.log("回调函数被调用")
                document.getElementById("usernameFeedback").innerText='';
                //elNameMsg.textContent='';
            }
        }
    }
}
function sendRequest(username){
    xhr=new XMLHttpRequest();
    xhr.onreadystatechange=process;
    xhr.open("GET",'checkUserName?username='+username);//记得修改链接，对应
    xhr.send(null);
}


// 密码匹配验证
elRpassword.addEventListener("blur", function() {
    var password = elPassword.value;
    var rpassword = elRpassword.value;

    if (password !== rpassword) {
        document.getElementById("passwordFeedback").innerText = '两次输入密码不同';
    } else {
        document.getElementById("passwordFeedback").innerText = '';
    }
});

